<?php

namespace Think;

class Memcachectr{
    
    private static $handel;
    
    private static $obj;
    
    private $timeout = 100;
    
    const _DB_PRE = "Mem_";
    
    private $_pre;
    
    //是否启用memcache
    private static $enable = true;
    
    public static function create($str = "default"){
        if(self::$enable == false){
            //...
        }
        if(!self::$obj instanceof Memcachectr){
            $re = new self($str);
            self::$obj = &$re;
        }
        return self::$obj;
    }
    
    private function __construct($str) {
        if($str!="" && self::$enable!=false){
            $h = new \Memcache();
            self::$handel = $h;
            if(!$str || $this->addSrever() === true){
                return;
            }
            $h->addserver(C('MEM_HOST'),C('MEM_PORT'),$this->timeout);
            self::$handel = &$h;
        }
    }
    
    private function addSrever(){
        if(!class_exists("Mem_group")){
            return false;
        }
        $mg = new Mem_group();
        if($mg->enable === false || sizeof($mg->group)<2){
            return false;
        }
        foreach($mg->group as $v){
            self::$handel->addServer($v["host"],$v["port"],$v["pconnect"],$v["weight"],$this->timeout,15,true);
        }
        return true;
    }
    
    public function set($key,$value,$bool=false,$time=1800){
        if(self::$enable == false){
            return false;
        }
        if($key == ""){
            return false;
        }
        $b = $bool?MEMCACHE_COMPRESSED:false;
        $re = self::$handel->set($this->_pre.$key,$value,$b,$time);
        return $re;
    }
    
    public function get($key){
        if(self::$enable == false){
            return false;
        }
        if($key == ""){
            return false;
        }
        $re = self::$handel->get($this->_pre.$key);
        return $re;
    }
    
    public function delete($key){
        if(self::$enable == false){
            return false;
        }
        if($key == ""){
            return false;
        }
        $re = self::$handel->delete($this->_pre.$key,$this->timeout);
        return $re;
    }
    
    public function replace($key,$value,$bool=false,$time=1800){
        if(self::$enable == false){
            return false;
        }
        if($key == ""){
            return false;
        }
        $re = @self::$handel->replace($this->_pre.$key,$value,$bool,$time);
        return $re;
    }
    
    public function increment($key,$value){
        if(self::$enable == false){
            return false;
        }
        if($key == ""){
            return false;
        }
        $re = @self::$handel->increment($this->_pre.$key,$value);
        return $re;
    }
    
    public function decrement($key,$value){
        if(self::$enable == false){
            return false;
        }
        if($key == ""){
            return false;
        }
        $re = @self::$handel->decrement($this->_pre.$key,$value);
        return $re;
    }
    
    public function flush(){
        return @self::$handel->flush();
    }
    
    public function __call($name,$args){
        exit("Exception: not found the function : $name");
    }
    
    private function __clone() {}
    
}
